<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform @deleteItem="deleteUserItem" ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr,errorModal} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				substanceIdCacheKey: 'zyk-sld-consumablesApply-title2-substanceId-CacheKey',
				userIdCacheKey: 'zyk-sld-consumablesApply-title2-userId-CacheKey',
				unitCache: 'zyk-sld-consumablesApply-title2-unit-CacheKey',
				applyUserId: '',
				applyUserName: '',
				applyInstId: '',
				applyInstName: '',
			})
		},
		methods:{
			initLoad(){
				let _that = this
				_that.$nextTick(function(){
					if(_that.formdata.substanceId){
						_that.$refs.autoform.changeEditor("title2", "substanceName","type","labelbox")
						_that.$refs.autoform.changeEditor("title2", "substanceModelSpecifications","type","labelbox")
						_that.$refs.autoform.changeEditor("title2", "unitId","type","labelbox")
					}
				})
			},
			clearSubstanceId(){
				let _that = this
				_that.$refs.autoform.clear('title2', 'substanceId');
				_that.$refs.autoform.clear('title2', 'substanceName');
				_that.$refs.autoform.clear('title2', 'substanceModelSpecifications');
				_that.$refs.autoform.clear('title2', 'brand');
				_that.$refs.autoform.clear('title2', 'unitId');
				_that.$refs.autoform.clear('title2', 'stockQuantity');
				_that.$refs.autoform.clear('title2', 'availableQuantity');
				_that.$refs.autoform.clear('title2', 'unitPrice');
				_that.$refs.autoform.clear('title2', 'issuedQuantity');
				_that.$refs.autoform.clear('title2', 'userId');
				_that.$refs.autoform.clear('title2', 'instId');
				_that.$refs.autoform.changeEditor("title2", "substanceName","type","textbox")
				_that.$refs.autoform.changeEditor("title2", "substanceModelSpecifications","type","textbox")
				_that.$refs.autoform.changeEditor("title2", "unitId","type","linkbox")
			},
			clearUserId(){
				let _that = this
				_that.$refs.autoform.clear('title2', 'userId');
				_that.$refs.autoform.clear('title2', 'instId');
			},
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.substanceIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.substanceId = d.id
							formdata.substanceCode = d.substanceCode
							formdata.substanceName = d.substanceName
							formdata.substanceModelSpecifications = d.substanceModelSpecifications
							formdata.brand = d.brand
							formdata.unitId = d.unitId
							formdata.unitName = d.unitName
							formdata.stockQuantity = d.stockQuantity
							formdata.availableQuantity = d.availableQuantity
							formdata.unitPrice = d.unitPrice
							formdata.issuedQuantity = d.issuedQuantity
							formdata.userId = _that.applyUserId
							formdata.userName = _that.applyUserName
							formdata.instId = _that.applyInstId
							formdata.instName = _that.applyInstName
							_that.$refs.autoform.changeEditor('title2', 'userId', {
								link:'../../public/pageSearch/pageSearch',
								linkParam: {
									other:JSON.stringify({
										firstUserId: _that.applyUserId
									}),
									url:'temployee/getStaffCombogrid',
									namefield:'userPosInstName',
									initload:true,
									searchfield:'q',
									cachename:_that.userIdCacheKey
								}
							});
							_that.om.request(
								'orgInstPosUserOfficial/findInstByUserIdOrPostId',
								{
									userId: _that.applyUserId
								}
							).then(res=>{
								_that.$refs.autoform.changeEditor("title2","instId","data",configSelectData(res,"id","instName"))
								_that.$refs.autoform.initSelectIndex("title2","instId")
							})
							
							_that.$refs.autoform.changeEditor("title2", "substanceName","type","labelbox")
							_that.$refs.autoform.changeEditor("title2", "substanceModelSpecifications","type","labelbox")
							_that.$refs.autoform.changeEditor("title2", "unitId","type","labelbox")
							uni.removeStorage({
								key:_that.substanceIdCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userIdCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userId = d.id
							formdata.userName = d.username
							_that.$refs.autoform.changeEditor('title2', 'userId', {
								link:'../../public/pageSearch/pageSearch',
								linkParam: {
									other:JSON.stringify({
										firstUserId: d.id
									}),
									url:'temployee/getStaffCombogrid',
									namefield:'userPosInstName',
									initload:true,
									searchfield:'q',
									cachename:_that.userIdCacheKey
								}
							});
							_that.om.request(
								'orgInstPosUserOfficial/findInstByUserIdOrPostId',
								{
									userId: d.id
								}
							).then(res=>{
								formdata.instId = d.salaryPosInstId
								formdata.instName = d.salaryPosInstName
								_that.$refs.autoform.changeEditor("title2","instId","data",configSelectData(res,"id","instName"))
								_that.$refs.autoform.initSelectIndex("title2","instId")
							})
							uni.removeStorage({
								key:_that.userIdCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.unitCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.unitId = d.unitId
							formdata.unitName = d.unitName
							uni.removeStorage({
								key:_that.unitCache
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			_that.applyUserId = e.applyUserId
			_that.applyUserName = e.applyUserName
			_that.applyInstId = e.applyInstId
			_that.applyInstName = e.applyInstName
			if(e.configDataCacheKey){
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							
							changeEditor('title2', "substanceId", {
								'link': '../../public/pageSearch/pageSearch',
								"linkParam": {
									url:'lowConsumablesInfoShow/getSubstancePage',
									namefield: 'substanceCode',
									notefield:'substanceName',
									searchfield: "q",
									initload:true,
									cachename: _that.substanceIdCacheKey,
								}
							}, null, _that.autoformdata)
							
							changeEditor('title2', 'userId', {
								link:'../../public/pageSearch/pageSearch',
								linkParam: {
									other:JSON.stringify({
										firstUserId: _that.formdata.userId
									}),
									url:'temployee/getStaffCombogrid',
									namefield:'userPosInstName',
									initload:true,
									searchfield:'q',
									cachename:_that.userIdCacheKey
								}
							}, null, _that.autoformdata);
							
							_that.om.request(
								'orgInstPosUserOfficial/findInstByUserIdOrPostId',
								{
									userId: _that.formdata.userId
								}
							).then(res=>{
								_that.$refs.autoform.changeEditor("title2","instId","data",configSelectData(res,"id","instName"))
								_that.$refs.autoform.initSelectIndex("title2","instId")
							})
							
							changeEditor("title2","unitId",{
								link:'../../public/pageSearch/pageSearch',
								"linkParam":{
									other:JSON.stringify({}),		
									url:'uomGroup/selectAllChildGrid',
									initload:true,
									searchfield:'q',
									namefield:'unitName',
									extrafield:'unitCode',
									notefield:'unitGroupName',
									cachename:_that.unitCache
								}
							},null,_that.autoformdata)
							
							

							_that.initLoad()
							
							}
						}
					})
				}
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
                                         